﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entidades;

namespace DAO
{
    public static class EntradaDAO
    {
        public static Entrada buscarPorId(int id)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBaseDatos"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            Entrada e = new Entrada();
            try
            {
                cn.Open();
                cmd.CommandText = "select id_Entrada,id_Pelicula,id_Cine,numero_Sala,id_Horario,fecha,id_Reserva,precio from Entrada where id_Reserva=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    e.IdEntrada = int.Parse(dr["id_Entrada"].ToString());
                    e.IdPelicula = int.Parse(dr["id_Pelicula"].ToString());
                    e.IdCine = int.Parse(dr["id_Cine"].ToString());
                    e.NumeroSala = int.Parse(dr["numero_Sala"].ToString());
                    e.IdHorario = int.Parse(dr["id_Horario"].ToString());
                    e.Fecha = DateTime.Parse(dr["fecha"].ToString());
                    e.IdReserva = int.Parse(dr["id_Reserva"].ToString());
                    e.Precio = double.Parse(dr["precio"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error al buscar  la entrada:" + ex.Message);

            }
            finally
            {
                cn.Close();
            }
            return e;

        }
        public static void darBajaEntradas(int id)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBaseDatos"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            try
            {
                cn.Open();
                cmd.Connection = cn;
                cmd.CommandText = "delete from Entrada where id_Entrada=@id";
                cmd.Parameters.Add(new SqlParameter("@id", id));
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Erroar al eliminar la entrada:" + ex.Message);

            }
            finally
            {
                cn.Close();

            }
        }
        public static List<Entrada> buscarPorIdReserva(int id)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBaseDatos"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            List<Entrada> en = new List<Entrada>();
            try
            {
                cn.Open();
                cmd.CommandText = "select id_Entrada,id_Pelicula,id_Cine,numero_Sala,id_Horario,fecha,id_Reserva,precio from Entrada where id_Reserva=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Entrada e = new Entrada();
                    e.IdEntrada = int.Parse(dr["id_Entrada"].ToString());
                    e.IdPelicula = int.Parse(dr["id_Pelicula"].ToString());
                    e.IdCine = int.Parse(dr["id_Cine"].ToString());
                    e.NumeroSala = int.Parse(dr["numero_Sala"].ToString());
                    e.IdHorario = int.Parse(dr["id_Horario"].ToString());
                    e.Fecha = DateTime.Parse(dr["fecha"].ToString());
                    e.IdReserva = int.Parse(dr["id_Reserva"].ToString());
                    e.Precio = double.Parse(dr["precio"].ToString());
                    en.Add(e);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error al buscar  la entrada:" + ex.Message);

            }
            finally
            {
                cn.Close();
            }
            return en;

        }
    }
}
